<template>
    <van-picker @change="onPickGener"
    title="性别" :columns="columns" @cancel="$emit('close')" @confirm="onConfirm" :default-index="gener" />
</template>

<script>
import { upUserProfileAPI } from '@/api/user'
import { ref } from 'vue'
import { Toast } from 'vant'
export default {
  name: 'editGener',
  props: {
    gener: {
      type: Number,
      required: true
    }
  },
  setup (props, content) {
    const columns = ['男', '女']
    const localGener = ref(props.gener)
    const onPickGener = (value, index) => {
      localGener.value = index
    }
    const onConfirm = async () => {
      Toast.loading({
        duration: 0,
        forbidClick: true,
        message: '修改中...'
      })
      try {
        await upUserProfileAPI({
          gender: localGener.value
        })
        // 修改父组件本地数据
        content.emit('upGener', localGener)
        // 关闭弹层
        content.emit('close')
        // 显示成功
        Toast.success('修改成功')
      } catch (err) {
        console.log(err)
      }
    }
    return {
      columns,
      onConfirm,
      onPickGener
    }
  }
}
</script>

<style scoped lang="less">

</style>
